package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.MyJoinBean;
import com.kaiyidiannao.widget.MyListview2;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.*;

/**
 * Created by wzg on 14-4-14.
 */
public class MyJoinAdapter extends ImageLoaderBaseAdapter implements Observer {

    private ListView listView;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<MyJoinBean> list = new ArrayList<MyJoinBean>();
    private String userid ;
    private ImageLoader imageLoader;
    private ArrayList<MyJoinBean> messageList = DataCenter.myJoinBeans;
    private int typeid = DataCenter.typeid;

    public MyJoinAdapter(Context context, List<MyJoinBean> list) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        TextView txt_people_count = null;
        final MyJoinBean myJoinBean = messageList.get(position);
        TextView title = null;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.my_join_item, null);
        }
        imageView = (ImageView) convertView.findViewById(R.id.imageView);
        title = (TextView) convertView.findViewById(R.id.title);
        txt_people_count = (TextView) convertView.findViewById(R.id.txt_people_count);
        imageLoader.displayImage(myJoinBean.getThumb(),imageView, options);
        title.setText(myJoinBean.getTitle());
//        txt_people_count.setText(myJoinBean.getTotal()+"人");
        txt_people_count.setTextColor(Color.WHITE);
      //  Button btn = (Button) convertView.findViewById(R.id.btn);

        final MyListview2 myListview = (MyListview2) convertView.findViewById(R.id.listView);
        DataCenter.myJoinPeopleListview = myListview;



        return convertView;
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        ((BaseActionBarActivity) context).closeProgressDialog();
        if (flag.equals(DataCenter.flag.GET_JOIN_IN_People_SUCCESS)) {
            DataCenter.myJoinPeopleListview.setVisibility(View.VISIBLE);
          //  ontext context, List<? extends Map<String, ?>> data,  int resource, String[] from, int[] to
            List<Map<String, String>> myData = new ArrayList<Map<String, String>>();
            for (int i = 0 ; i < DataCenter.myJoinPeopleBeans.size(); ++i) {
                Map<String, String> map = new HashMap<String, String>();
                myData.add(map);
                map.put("username", DataCenter.myJoinPeopleBeans.get(i).getUsername());
                map.put("mobile", DataCenter.myJoinPeopleBeans.get(i).getMobilephone());
            }

            SimpleAdapter simpleAdapter =
                    new SimpleAdapter(context, myData, R.layout.my_join_people,
                            new String[] {"username", "mobile"}, new int[] {R.id.username, R.id.mobile});
            DataCenter.myJoinPeopleListview.setAdapter(simpleAdapter);
        } else if(flag.equals(DataCenter.flag.GET_JOIN_IN_PEOPLE_FAILED)) {
            ((BaseActionBarActivity) context).showToast( context.getString(R.string.listview_msg_error));
        }
    }
}
